//
//  CS_QueryPanel_OtherConditionsView.m
//  CSUIKit
//
//  Created by 浙大网新中研软件 on 2018/3/23.
//

#import "CS_QueryPanel_OtherConditionsView.h"
#import "CS_Page_TextSelectView.h"

#define MARGIN 10.0
#define HEIGHT_CONDITION_BUTTON 35.0

@interface CS_QueryPanel_OtherConditionsView()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIButton *saveButton;
@property (nonatomic, strong) UIButton *clearButton;

@property (nonatomic, strong) NSMutableArray *displayViews;

@end

@implementation CS_QueryPanel_OtherConditionsView

-(CGFloat)layoutView:(UIView *)superView primaryY:(CGFloat)primaryY{
    
    CGFloat localPrimaryY = 0.0;
    
    for (int i = 0; i < self.otherConditions.count; i++) {
        
        CSConditionModel *conditionModel = [self.otherConditions objectAtIndex:i];
        __weak CSConditionModel *weakConditionModel = conditionModel;
        
        if (conditionModel.type == CSViewType_PageSingleView) {
            
            //单选
            CS_Page_TextSelectView *page_TextSelectView = [CS_Page_TextSelectView pageTextSelectView:conditionModel.placeHolder selectItems:conditionModel.selectItems];
            page_TextSelectView.selectHandler2 = ^(NSInteger section, NSInteger index, CSSelectableModel *model){
                
                weakConditionModel.value = model.code;
                weakConditionModel.contentValue = model.content;
                
            };
            localPrimaryY = [page_TextSelectView layoutView:self.scrollView primaryY:localPrimaryY];
            [self.displayViews addObject:page_TextSelectView];
            
        }else if(conditionModel.type == CSViewType_TimeRangeView){
            
            CS_Page_TimeRangeView *page_TimeRangeView = [CS_Page_TimeRangeView page_TimeRangeViewWithPlaceHolder:conditionModel.placeHolder defaultStartDate:nil defaultEndData:nil];
            localPrimaryY = [page_TimeRangeView layoutView:self.scrollView primaryY:localPrimaryY];
            [self.displayViews addObject:page_TimeRangeView];
            
        }else if(conditionModel.type == CSViewType_PageMulityView){
            
        }
    }
    
    localPrimaryY += MARGIN;
    
    CGFloat buttonWidth = (kWindowW - 2 * CELL_EDIT_RIGHT_MARGIN - MARGIN) / 2.0;
    self.clearButton.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN, localPrimaryY, buttonWidth, HEIGHT_CONDITION_BUTTON);
    self.saveButton.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN + buttonWidth + MARGIN, localPrimaryY, buttonWidth, HEIGHT_CONDITION_BUTTON);
    [self.scrollView addSubview:self.clearButton];
    [self.scrollView addSubview:self.saveButton];
    localPrimaryY += 55;
    
    self.backgroundColor = [UIColor whiteColor];
    
    CGFloat localHeight = localPrimaryY;
    
    if (localPrimaryY > kWindowH - primaryY - 150) {
        localHeight = kWindowH - primaryY - 150;
        
        //背景视图
        UIView *backView = [UIView new];
        backView.backgroundColor = [UIColor whiteColor];
        backView.frame = CGRectMake(0, localHeight - 55, kWindowW, 55);
        [self addSubview:backView];
        
        self.clearButton.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN, 10, buttonWidth, HEIGHT_CONDITION_BUTTON);
        self.saveButton.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN + buttonWidth + MARGIN, 10, buttonWidth, HEIGHT_CONDITION_BUTTON);
        [backView addSubview:self.clearButton];
        [backView addSubview:self.saveButton];
        
    }
    
    self.frame = CGRectMake(0, primaryY, kWindowW, kWindowH - primaryY);
    self.scrollView.contentSize = CGSizeMake(0, localHeight);
    self.scrollView.frame = CGRectMake(0, 0, kWindowW, localHeight);
    self.scrollView.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:self.scrollView];
    
    self.maskButton.frame = CGRectMake(0, 0, kWindowW, kWindowH);
    [self insertSubview:self.maskButton atIndex:0];
    
    [superView addSubview:self];
    
    return primaryY;
}

-(void)clearClick{
    
    for (int i = 0; i < self.displayViews.count; i++) {
        CSBaseView *baseView = [self.displayViews objectAtIndex:i];
        [baseView reset];
    }
    
}

-(void)saveClick{
    
    if (self.onMoreConditionConfigureFinishedHandler) {
        self.onMoreConditionConfigureFinishedHandler();
    }
    
}

-(UIScrollView *)scrollView{
    
    if (_scrollView == nil) {
        _scrollView = [UIScrollView new];
        _scrollView.bounces = NO;
    }
    return _scrollView;
    
}

-(UIButton *)clearButton{
    
    if (_clearButton == nil) {
        _clearButton = [[UIButton alloc] init];
        [_clearButton setTitle:@"清空" forState:UIControlStateNormal];
        [_clearButton addTarget:self action:@selector(clearClick) forControlEvents:UIControlEventTouchUpInside];
        STYLE_SAVE_BUTTON(_clearButton);
        
        _clearButton.backgroundColor = [UIColor colorFromHexCode:@"f5f5f5"];
        _clearButton.layer.borderColor = [UIColor colorFromHexCode:@"e4e4e4"].CGColor;
        _clearButton.layer.borderWidth = 1.0;
        [_clearButton setTitleColor:[UIColor colorFromHexCode:@"444444"] forState:UIControlStateNormal];
    }
    return _clearButton;
    
}

-(UIButton *)saveButton{
    
    if (_saveButton == nil) {
        _saveButton = [[UIButton alloc] init];
        [_saveButton setTitle:@"确定" forState:UIControlStateNormal];
        [_saveButton addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
        STYLE_SAVE_BUTTON(_saveButton);
    }
    return _saveButton;
    
}

-(NSMutableArray *)displayViews{
    
    if (_displayViews == nil) {
        _displayViews = [NSMutableArray new];
    }
    return _displayViews;
    
}

@end
